// 初始化重命名按钮事件
$(function() {
    // 点击重命名按钮
    $('.rename-btn').click(function() {
        const fileId = $(this).data('file-id');
        const fileName = $(this).data('file-name');
        const filePath = $(this).data('file-path');

        $('#rename-file-id').val(fileId);
        $('#rename-old-path').val(filePath);
        $('#new-file-name').val(fileName);
        $('#name-error').hide();
        $('#rename-dialog').modal('show');
    });

    // 确认重命名
    $('#confirm-rename').click(submitRename);

    // 输入框实时校验
    $('#new-file-name').on('input', function() {
        const val = $(this).val().trim();
        const illegalChars = /[\\/:*?"<>|]/;
        if (illegalChars.test(val)) {
            $('#name-error').text('[[#{file.rename.illegal}]]').show();
        } else if (val.length > 255) {
            $('#name-error').text('[[#{file.rename.too.long}]]').show();
        } else {
            $('#name-error').hide();
        }
    });
});

// 提交重命名请求
function submitRename() {
    const fileId = $('#rename-file-id').val();
    const newName = $('#new-file-name').val().trim();
    const oldPath = $('#rename-old-path').val();

    if (!newName) {
        $('#name-error').text('[[#{file.name.not.empty}]]').show();
        return;
    }

    $.ajax({
        type: 'POST',
        url: '/file/rename',
        dataType: 'json',
        data: { fileId, newName, oldPath },
        success: function(response) {
            if (response.success) {
                $('#rename-dialog').modal('hide');
                syncFileData(); // 调用现有方法刷新文件列表
                showMessage('success', response.message);
            } else {
                $('#name-error').text(response.message).show();
            }
        },
        error: function() {
            $('#name-error').text('[[#{common.server.error}]]').show();
        }
    });
}
// 提交文件上传
function submitUpload() {
    var form = $("#upload-form");
    form.ajaxSubmit({
        url: '/file/upload',
        type: 'POST',
        dataType: 'json',
        beforeSubmit: function () {
            // 验证是否选择文件
            var files = $("#file-upload")[0].files;
            if (files.length === 0) {
                showMessage('error', '[[#{file.upload.select.empty}]]');
                return false;
            }
            return true;
        },
        success: function (response) {
            if (response.success) {
                $("#upload-dialog").dialog("close");
                syncFileData(); // 上传成功后刷新文件列表
                showMessage('success', '[[#{file.upload.success}]]');
            } else {
                showMessage('error', response.message || '[[#{file.upload.error}]]');
            }
        },
        error: function () {
            showMessage('error', '[[#{common.server.error}]]');
        }
    });
}

/**
 * 浏览文件
 */
function browseFile() {
    var ids = $("#flexigrid-id").getFlexigridiAttrByIndex(0);
    if (ids.length === 0) {
        message(/*[[#{common.empty}]]*/'请选择文件');
        return;
    }

    if (ids.length > 1) {
        message(/*[[#{share.browse.multi.select}]]*/'只能选择一个文件进行浏览');
        return;
    }

    var fileId = ids[0];
    // 获取文件信息
    $.ajax({
        type: 'post',
        dataType: 'json',
        url: /*[[@{/share/queryFileInfoById}]]*/'',
        data: [
            {name: "fileUuid", value: fileId}
        ],
        success: function (data) {
            if (data) {
                if (data.fileType === FILE_TYPE_FOLDER) {
                    // 如果是文件夹，进入该文件夹
                    enterDir(fileId);
                } else {
                    // 如果是文件，打开文件浏览窗口
                    openFileBrowser(fileId, data.fileName);
                }
            }
        },
        error: function (msg) {
            message(/*[[#{common.error}]]*/'操作失败');
        }
    });
}